<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="PUSH内容">
              <el-input v-model="form.content" placeholder="请输入PUSH内容" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="PUSH类型">
              <el-select v-model="form.type" placeholder="请选择PUSH类型" clearable filterable>
                <el-option
                  v-for="item in pushCate"
                  :label="item.name"
                  :value="item.id"
                  :key="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="PUSH地区">
              <el-cascader v-model="address" :options="twoOptions" clearable filterable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="form.status" placeholder="请选择审核状态" clearable>
                <el-option
                  :label="item.name"
                  :value="item.value"
                  v-for="item in statusList"
                  :key="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="20">
            <el-button type="primary" @click="searchList()">查询</el-button>
            <el-button class="leadingOut" type="primary" @click="toRoutes('pushManageAdd')">新增</el-button>
            <!-- <el-button type="primary" @click="handleExport">导出</el-button> -->
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed type="index" :index="table_index" label="编号" width="80px"></el-table-column>
        <el-table-column prop="title" label="Push标题"></el-table-column>
        <el-table-column prop="content" label="Push内容"></el-table-column>
        <el-table-column prop="type" label="Push类型">
          <!-- <template slot-scope="scope">
            <span>{{scope.row.check_status}}</span>
          </template>-->
        </el-table-column>
        <el-table-column prop="province_name" label="地区">
          <!-- <template slot-scope="scope">
            {{scope.row.province_name + scope.row.city_name}}
            <br />
          </template>-->
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">{{scope.row.status}}</template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="toRoutes('pushManagedetail',scope.row.id)">查看</el-button>
            <el-button
              v-if="scope.row.status.indexOf('下')!=-1"
              type="text"
              size="mini"
              @click="toRoutes('pushManageEdit',scope.row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              size="mini"
              v-if="scope.row.status.indexOf('上')!=-1"
              @click="UpperStatus(scope.row.id,scope.row.content,0)"
            >下架</el-button>
            <el-button
              type="text"
              v-else
              @click="UpperStatus(scope.row.id,scope.row.content,1)"
              size="mini"
            >上架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.page"
            :page-sizes="pageSizes"
            :page-size="form.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
      <!-- 
      <el-dialog
        title
        v-if="dialogFormVisible"
        :visible.sync="dialogFormVisible"
        width="30%"
        @close="clear"
      >
        <el-form ref="form1" :model="form1" label-width="100px" :rules="rules">
          <el-form-item label="驳回原因" v-if="form1.check_status==2" prop="remark">
            <span></span>
          </el-form-item>
        </el-form>
        <div slot="footer" center class="dialog-footer">
            <el-button type="primary" @click="dialogFormVisible=false">取消</el-button>
          <el-button type="primary" @click="checkStatus('form1')">确定</el-button>
        </div>
      </el-dialog>-->
    </div>
  </div>
</template>
<script>
import { pushList, getPro, UpperStatus, handleExport } from "@/api/pushManage";
import { exportfile } from "@/api/exporfile";

export default {
  name: "pushManageIndex",
  data() {
    return {
      isShow: false,
      userName: "",
      // loading层
      tableLoading: false,
      //地区
      address: [],
      // 提交form
      form: {
        content: "",
        // province_name: "",
        // city: "",
        province_id: "",
        type: "",
        status: "",
        page: 1,
        pageSize: 10
      },
      // dialogFormVisible: false,
      // 状态下拉列表  0 下架 1 上架
      statusList: [
        {
          name: "全部",
          value: ""
        },
        {
          name: "下架",
          value: "0"
        },
        {
          name: "上架",
          value: "1"
        }
      ],
      //push分类
      pushCate: [
        { id: "1", name: "默认消息" },
        { id: "2", name: "H5链接" },
        { id: "3", name: "商品关键词" },
        { id: "4", name: "项目关键词" },
        { id: "5", name: "项目分类" },
        { id: "6", name: "商品分类" },
        { id: "7", name: "plus主页" },
        { id: "8", name: "生美主页" }
      ],
      twoOptions: [{ label: "全国", value: 0 }], //省市
      // 主页列表显示
      tableData: [],
      //弹框提示信息
      UpperInfo: {},
      // 分页
      pageSizes: [10, 30, 50, 100],
      page_totals: 10
    };
  },
  inject: ["reload"],
  watch: {
    // province_name: function(val) {
    //   if (val) {
    //     this.form.province = val[0];
    //     this.form.city = val[1];
    //   }
    // },
    address: function(val) {
      // console.log(val);
      if (val) {
        // this.form.province_name = val[0];
        // this.form.city = val[1];
        this.form.province_id = val[0];
      }
    }
  },
  //上下架状态过滤 0 下架 1 上架
  filters: {
    // statusFilter(e) {
    //   switch (e) {
    //     // case "1":
    //     //   return "待上架";
    //     //   break;
    //     case "1":
    //       return "上架";
    //       break;
    //     case "0":
    //       return "下架";
    //       break;
    //     default:
    //       return "暂无";
    //       break;
    //   }
    // }
  },

  created() {
    this.getArea();
  },
  mounted() {
    this.getList(this.form);
  },
  methods: {
    table_index(index) {
      return (this.form.page - 1) * this.form.pageSize + index + 1;
    },
    // push省市区
    getArea() {
      getPro().then(res => {
        const newArr = [];
        // console.log(res);
        res.data.forEach((item1, index) => {
          newArr.push({
            value: item1.value,
            label: item1.label
          });
          if (item1.children != null) {
            newArr[index]["children"] = [];
            item1.children.forEach(item2 => {
              newArr[index]["children"].push({
                value: item2.value,
                label: item2.label
              });
            });
          }
        });
        // this.options = res.data;
        newArr.unshift({
          label: "全国",
          value: 0
        });
        this.twoOptions = newArr;
      });
    },
    // 获取列表
    getList(e) {
      this.tableLoading = true;
      pushList(e)
        .then(res => {
          console.log(res);
          if (res.data.code == 200) {
            this.tableData = res.data.data.data;
            this.page_totals = res.data.data.total;
            this.tableLoading = false;
            // console.log(this.page_totals)
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err.message);
          this.tableLoading = false;
        });
    },
    //查询
    searchList() {
      this.form.page = 1;
      this.getList(this.form);
    },
    //弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    //成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 警告状态弹窗
    warningStatus(txt) {
      this.eleMsg(txt, "warning");
    },
    // 路由跳转
    toRoutes(e, userId) {
      let route = { name: e, query: { id: userId } };
      this.$router.push(route);
    },
    // 导出
    //导出
    handleExport() {
      let datas = { ...this.form };
      handleExport(this.form).then(res => {
        exportfile(res);
      });
    },
    // handleExport() {
    // cardExport({
    //   name: this.form.name,
    //   category_id: this.form.category_id,
    //   province: this.form.province,
    //   city: this.form.city,
    //   update_status: this.form.update_status
    // })
    //   .then(res => {
    //     commonFn.downFile("商户审核列表", res);
    //   })
    //   .catch(err => {
    //     this.errorStatus(err);
    //   });
    // },

    // 取消弹窗
    // cancel() {
    //   this.dialogFormVisible = false;
    //   this.$message("已取消");
    // },

    // 上下架状态修改
    UpperStatus(id, name, status) {
      this.UpperInfo = { id, status };
      if (status == 1) {
        this.$confirm("确定上架" + "'" + name + "'" + "该push吗?", "上架提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        })
          .then(() => {
            UpperStatus(this.UpperInfo)
              .then(res => {
                // console.log(res)
                if (res.data.code == 200) {
                  this.reload();
                  this.$message({
                    type: "success",
                    message: res.data.msg
                  });
                }
              })
              .catch(err => {
                this.errorStatus(err.msg);
                this.tableLoading = false;
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$confirm("确定下架" + "'" + name + "'" + "该push吗?", "下架提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        })
          .then(() => {
            UpperStatus(this.UpperInfo).then(res => {
              //
              if (res.data.code == 200) {
                this.reload();
                this.$message({
                  type: "success",
                  message: res.data.msg
                });
                //
              }
            });
            // .catch(err => {
            //   this.errorStatus(err.msg);
            this.tableLoading = false;
            // });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      }
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
/* .el-range-editor--medium.el-input__inner {
  width: 236px;
} */
.switchBtn {
  color: #ff4949;
}
.el-radio-group {
  margin-left: 30px;
}
img {
  display: block;
  width: 80px;
  height: 80px;
}
</style>